import os
from random import choice


def show():
    global sta
    for i in range(3):
        for j in range(3):
            print(sta[i * 3 + j], end='\t| ')
        print()


def judge():
    global detail
    global sta
    a = True
    detail = ''
    # 判定行
    for i in range(3):
        if sta[i * 3] == sta[i * 3 + 1] == sta[i * 3 + 2] != '':
            a = False
            detail = sta[i * 3]
    # 判定列
    for i in range(3):
        if sta[i] == sta[i + 3] == sta[i + 6] != '':
            a = False
            detail = sta[i]
    # 判定对角线
    if sta[0] == sta[4] == sta[8] != '' or sta[2] == sta[4] == sta[6] != '':
        a = False
        detail = sta[4]
    if '' not in sta:
        a = False
    return a


def think():
    global sta
    result = -1
    for i in range(3):
        if sta[i * 3] == sta[i * 3 + 1] == 'x' and sta[i * 3 + 2] == '':
            result = i * 3 + 2
    for i in range(3):
        if sta[i * 3] == sta[i * 3 + 2] == 'x' and sta[i * 3 + 1] == '':
            result = i * 3 + 1

    for i in range(3):
        if sta[i * 3 + 1] == sta[i * 3 + 2] == 'x' and sta[i * 3] == '':
            result = i * 3

    for i in range(3):
        if sta[i] == sta[i + 3] == 'x' and sta[i + 6] == '':
            result = i + 6

    for i in range(3):
        if sta[i] == sta[i + 6] == 'x' and sta[i + 3] == '':
            result = i + 3

    for i in range(3):
        if sta[i + 3] == sta[i + 6] == 'x' and sta[i] == '':
            result = i

    if sta[0] == sta[4] == 'x' and sta[8] == '':
        result = 8
    if sta[0] == sta[8] == 'x' and sta[4] == '':
        result = 4
    if sta[4] == sta[8] == 'x' and sta[0] == '':
        result = 0
    if sta[2] == sta[4] == 'x' and sta[6] == '':
        result = 6
    if sta[0] == sta[6] == 'x' and sta[4] == '':
        result = 4
    if sta[4] == sta[6] == 'x' and sta[2] == '':
        result = 2

    for i in range(3):
        if sta[i * 3] == sta[i * 3 + 1] == 'o' and sta[i * 3 + 2] == '':
            result = i * 3 + 2

    for i in range(3):
        if sta[i * 3] == sta[i * 3 + 2] == 'o' and sta[i * 3 + 1] == '':
            result = i * 3 + 1

    for i in range(3):
        if sta[i * 3 + 1] == sta[i * 3 + 2] == 'o' and sta[i * 3] == '':
            result = i * 3

    for i in range(3):
        if sta[i] == sta[i + 3] == 'o' and sta[i + 6] == '':
            result = i + 6

    for i in range(3):
        if sta[i] == sta[i + 6] == 'o' and sta[i + 3] == '':
            result = i + 3

    for i in range(3):
        if sta[i + 3] == sta[i + 6] == 'o' and sta[i] == '':
            result = i

    if sta[0] == sta[4] == 'o' and sta[8] == '':
        result = 8
    if sta[0] == sta[8] == 'o' and sta[4] == '':
        result = 4
    if sta[4] == sta[8] == 'o' and sta[0] == '':
        result = 0
    if sta[2] == sta[4] == 'o' and sta[6] == '':
        result = 6
    if sta[0] == sta[6] == 'o' and sta[4] == '':
        result = 4
    if sta[4] == sta[6] == 'o' and sta[2] == '':
        result = 2

    if result == -1:
        if sta == ['' for i in range(9)]:
            result = 4
        else:
            empty_positions = [i for i, x in enumerate(sta) if x == '']
            if empty_positions:
                result = choice(empty_positions)
    return result


detail = ''
sta = ['' for i in range(9)]
# sta = ['x', 'o', 'x', 'x', 'o', 'o', 'o', 'x', 'o']
print('这是一个井字棋程序')

while True:
    try:
        player = int(input('谁先手？(0为你，1为机器人)'))
        break
    except ValueError:
        print('请输入0或1')
while judge():
    if os.name == 'nt':
        if player % 2 == 1:
            os.system('color 9')
        elif player % 2 == 0:
            os.system('color a')
    # print('本次是{}玩家'.format('o' if player % 2 == 0 else 'x'))
    if player % 2 == 0:
        print('\n现在是玩家(o)的回合')
        try:
            x, y = map(int, input('请告诉我你要填入第几行第几列: ').split())
        except ValueError:
            print('你输入的不是用空格分隔的两个数字')
            continue
        try:
            if sta[(x - 1) * 3 + y - 1] != '':
                print('你选择的是已填充的')
            else:
                sta[(x - 1) * 3 + y - 1] = 'o'
                player += 1
                show()
        except IndexError:
            print('请确保输入的数范围在1~3以内')
            continue
    else:
        print('\n现在是计算机(x)的回合')
        print('计算机正在思考……\n')
        sta[think()] = 'x'
        player += 1
        print('计算机思考了片刻，并进行了填充')
        show()
print()
if '' not in sta:
    if os.name == 'nt':
        os.system('color d')
    print('该局平局')
else:
    if detail == 'o':
        if os.name == 'nt':
            os.system('color e')
        print('恭喜你，获得了胜利')
    else:
        if os.name == 'nt':
            os.system('color c')
        print('真可惜，机器人获胜')
print('Game Over')
input('按Enter以退出')
